<?php
namespace app\beautiful\controller\admin\project;
use app\BaseController;
use app\common\logic\DataLogic;
use app\beautiful\model\project\ProjectModel;
use app\beautiful\model\project\ProjectTypeModel;
use app\common\model\ShopStoreModel;

class Project extends BaseController{
    protected $name='项目';
    
    public function index()
    {
        $abbreviation=$this->request->param('abbreviation');
        $project_type_id=$this->request->param('project_type_id/d');
        $bg_take_count=$this->request->param('bg_take_count/d');
        $end_take_count=$this->request->param('end_take_count/d');
        $store_id = $this->request->param('store_id/d',0);
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];

        if(!empty($bg_take_count)){
            if(!empty($end_take_count)){
                $where[]=['take_count','between',[$bg_take_count,$end_take_count]];
            }else{
                $where[]=['take_count','>=',$bg_take_count];
            }
        }else{
            if(!empty($end_take_count)){
                $where[]=['take_count','<=',$end_take_count];
            }
        }
        
        if(!empty($abbreviation)) $where[]=['abbreviation','=',$abbreviation];
        if(!empty($project_type_id)) $where[]=['project_type_id','=',$project_type_id];
        
        $project_model= new ProjectModel();
        $project_model=$project_model->with(['type']);
        $order=['add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($project_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        $total = $return['total'];
        if(!empty($return['list']) && $store_id > 0) {
            foreach($return['list'] as $key => $value) {
                if(empty($value['store'])) {
                    unset($return['list'][$key]);
                    $total = $total - 1;
                }
                $store = explode(',',$value['store']);
                if(!in_array($store_id,$store)) {
                    unset($return['list'][$key]);
                    $total = $total - 1;
                }
            }
            $return['list'] = array_values($return['list']);
            $return['total'] = $total;
        }
        
        $where_store = [
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['enable','=',1],
        ];
        $inster = ['id'=>0,'name'=>'总部'];
        $field='id,name';
        $order=['is_main'=>'asc','add_time'=>'desc'];
        $store = ShopStoreModel::where($where_store)->field($field)->order($order)->select()->toArray();
        $store = array_merge($inster,$store);
        $return['store'] = !empty($store) ? array_values($store) : [];
        $return['store_id'] = $store_id;
        if($this->request->page==1){
            $type_list=ProjectTypeModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0]
            ])->field('project_type_id,name')->select()->toArray();
            $return['type_list']=$type_list;
        }

        return $this->success($return);

    }

    public function show()
    {
        $return=[];
        $project_id = $this->request->param('project_id/d',0);
        
        $type_list=ProjectTypeModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ])->field('project_type_id,name')->select()->toArray();
        $return['type_list']=$type_list;
        $return['store'] = [];
        if($project_id==0) return $this->success($return);
        if(null === $project = ProjectModel::find($project_id)) return $this->error($this->name.'不存在');
        if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($project->is_delete != 0) return $this->error($this->name.'不存在');
        
        $detail=[
            'project_type_id'=>$project->project_type_id,
            'full_name'=>$project->full_name,
            'abbreviation'=>$project->abbreviation,
            'market_price'=>$project->market_price,
            'cover_img'=>$project->cover_img,
            'project_details'=> !empty($project->project_details) ? json_decode($project->project_details,true):[],
        ];
        
        if(!empty($project->store)) {
            $store = explode(',',$project->store);
            foreach($store as &$v) {
                $v = intval($v);
            }
            $return['store'] = $store;
        }
        $return['detail']=$detail;
        return $this->success($return);

    }

    public function save()
    {
        $project_id = $this->request->param('project_id/d',0);
        $full_name=$this->request->param('full_name');
        $project_type_id=$this->request->param('project_type_id/d',0);
        $abbreviation=$this->request->param('abbreviation');
        $market_price=$this->request->param('market_price/f',0);
        $cover_img=$this->request->param('cover_img');
        $store_id = $this->request->param('store_id/a',[]);
        $project_details=$this->request->param('project_details','','security_json_or_array');
        if(empty($full_name)) return $this->error('请填写全称');
        if(empty($abbreviation)) return $this->error('请填写简称');
        if(empty($project_type_id)) return $this->error('请选择分类');
        if(null === ProjectTypeModel::find($project_type_id)) return $this->error('请选择分类');
        if(empty($market_price) || $market_price<=0) return $this->error('请输入市场价，且不能小于0');
        if(empty($cover_img)) return $this->error('请上传封面');
        if(empty($store_id)) return $this->error('请选择关联门店');
        if($project_id!=0){
            if(null === $project = ProjectModel::find($project_id)) return $this->error($this->name.'不存在');
            if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
            if($project->is_delete != 0) return $this->error($this->name.'不存在');
            $pre_cover_img=$project->cover_img;
        }else{
            $project = new ProjectModel();
            $project->shop_id = $this->request->shopId;
            $pre_cover_img=null;
        }
        $project->full_name=$full_name;
        $project->abbreviation=$abbreviation;
        $project->project_type_id=$project_type_id;
        $project->market_price=$market_price;
        $project->cover_img=$cover_img;
        $project->store = implode(',',$store_id);
        $project->project_details = !empty($project_details) ? json_encode($project_details) : $project_details;
        $project->save();

        //图片处理
        imgReplaceAct($cover_img,$pre_cover_img);
        return $this->success('保存成功');
    }

    public function del()
    {
        $project_id = $this->request->param('project_id/d',0);
        if($project_id==0) return $this->error($this->name.'不存在');

        if(null === $project = ProjectModel::find($project_id)) return $this->error($this->name.'不存在');
        if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($project->is_delete != 0) return $this->error($this->name.'不存在');

        //判断抢购 后期补充

        //判断次卡 后期补充

        $project->is_delete =1 ;
        $project->save();
        return $this->success('删除成功');

    }


    /********************************************************* 项目分类 *****************************************************************/
    public function getProjectTypeList()
    {
        $name=$this->request->param('name');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        if(!empty($name)) $where[]=['name','=',$name];
        $project_type_model = new ProjectTypeModel();
        $project_type_model=$project_type_model->withCount(['project'=>function($query){
            $query->where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0]
            ]);
        }]);
        $order=['sort'=>'desc','add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($project_type_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function showProjectType()
    {
        $project_type_id=$this->request->param('project_type_id/d',0);
        if($project_type_id==0) return $this->success([]);

        if(null === $project_type = ProjectTypeModel::find($project_type_id)) return $this->error('分类不存在');
        if($project_type->shop_id != $this->request->shopId) return $this->error('分类不存在');
        if($project_type->is_delete != 0) return $this->error('分类不存在');

        $detail=[
            'name'=>$project_type->name,
            'sort'=>$project_type->sort,
        ];

        return $this->success(['detail'=>$detail]);
    }

    public function saveProjectType()
    {
        $project_type_id=$this->request->param('project_type_id/d',0);
        $name=$this->request->param('name');
        $sort=$this->request->param('sort/d',0);
        if(empty($name)) return $this->error('请填写分类名称');
        if($sort<0) return $this->error('请填写排序，且不能小于0');
        if($project_type_id != 0){
            if(null === $project_type = ProjectTypeModel::find($project_type_id)) return $this->error('分类不存在');
            if($project_type->shop_id != $this->request->shopId) return $this->error('分类不存在');
            if($project_type->is_delete != 0) return $this->error('分类不存在');

            if($project_type->name != $name){
                if(null !== ProjectTypeModel::where([
                        ['shop_id','=',$this->request->shopId],
                        ['is_delete','=',0],
                        ['name','=',$name]
                    ])->find()){
                    return $this->error('该分类名称已存在，请重新输入');
                }
            }
        }else{
            if(null !== ProjectTypeModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0],
                    ['name','=',$name]
                ])->find()){
                return $this->error('该分类名称已存在，请重新输入');
            }
            $project_type = new ProjectTypeModel();
            $project_type->shop_id= $this->request->shopId;
        }
        $project_type->name=$name;
        $project_type->sort=$sort;
        $project_type->save();

        return $this->success('操作成功');

    }

    public function delProjectType()
    {
        $project_type_id=$this->request->param('project_type_id/d',0);
        if($project_type_id==0) return $this->error('分类不存在');

        if(null === $project_type = ProjectTypeModel::find($project_type_id)) return $this->error('分类不存在');
        if($project_type->shop_id != $this->request->shopId) return $this->error('分类不存在');
        if($project_type->is_delete != 0) return $this->error('分类不存在');

        $project_count= ProjectModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['project_type_id','=',$project_type_id]
        ])->count() ?? 0;

        if($project_count!=0) return $this->error('分类下面还有项目，无法删除');
        $project_type->is_delete = 1;
        $project_type->save();
        return $this->success('删除成功');
    }
}
